function getDatabase() {
     return openDatabaseSync("todoBList", "1.0", "StorageDatabase", 100000);
}


// At the start of the application, we can initialize the tables we need if they haven't been created yet
function initialize() {
    var db = getDatabase();
    db.transaction(
        function(tx) {
            // Create the settings table if it doesn't already exist
            // If the table exists, this is skipped
            tx.executeSql('CREATE TABLE IF NOT EXISTS todoB(date TEXT, what TEXT, due TEXT, priority TEXT)');
      });
}


// This function is used to write a setting into the database
function addDate(date, what, due, priority) {
   var db = getDatabase();
   var res = "";
   db.transaction(function(tx) {
        var rs = tx.executeSql('INSERT INTO todoB VALUES (?,?,?,?);', [date,what,due,priority]);
              if (rs.rowsAffected > 0) {
                res = "OK";
                  console.log("OK");
              } else {
                res = "Error";
                  console.log("ERROR");
              }
        }
  );
  // The function returns “OK” if it was successful, or “Error” if it wasn't
  return res;
}

// This function is used to delete a Data from the database
function deleteDate(date, what) {
   var db = getDatabase();
   var res = "";
   db.transaction(function(tx) {
        var rs = tx.executeSql('DELETE FROM todoB WHERE date=? AND what=?;', [date, what]);
              //console.log(rs.rowsAffected)
              if (rs.rowsAffected > 0) {
                res = "OK";
                  console.log("OK");
              } else {
                res = "Error";
                  console.log("ERROR");
              }
        }
  );
  // The function returns “OK” if it was successful, or “Error” if it wasn't
  return res;
}

function deleteWhat(what) {
   var db = getDatabase();
   var res = "";
   db.transaction(function(tx) {
        var rs = tx.executeSql('DELETE FROM todoB WHERE what=? AND ;', [what]);
              //console.log(rs.rowsAffected)
              if (rs.rowsAffected > 0) {
                res = "OK";
                  console.log("OK");
              } else {
                res = "Error";
                  console.log("ERROR");
              }
        }
  );
  // The function returns “OK” if it was successful, or “Error” if it wasn't
  return res;
}

function getDate(date) {
   var db = getDatabase();
   var res="";
   var stringVar=new Array();

   db.transaction(function(tx){
     var rs = tx.executeSql('SELECT * FROM todoB WHERE date=?;', [date]);
     if (rs.rows.length > 0) {
         for(var  index=0; index<rs.rows.length; index++){
             //for(var  col=0; col<3; col++){
                 //RETRIEVE WHAT
                res = rs.rows.item(index).what;
                stringVar[index]=res;
             //    console.log("WHAT: " + res);
                 //RETRIEVE DUE DATE
            //    res = rs.rows.item(col).due;
            //    stringVar[index]=stringVar[index]+" " + res;
           //      console.log("DUE: " + res);
          //       //RETRIEVE PRIORITY
           //     res = rs.rows.item(col).priority;
           //      stringVar[index]=stringVar[index]+" " + res;
          //       console.log("PRIORITY: " + res);
//
          //       console.log("DERP:" + stringVar[index]);
             //}
         }
     } else {
         res = "Unknown";
     }
  })
    return stringVar;
}

function getDue(date) {
   var db = getDatabase();
   var res="";
   var stringVarD=new Array();

   db.transaction(function(tx){
     var rs = tx.executeSql('SELECT * FROM todoB WHERE date=?;', [date]);
     if (rs.rows.length > 0) {
         for(var  index=0; index<rs.rows.length; index++){
            // for(var  col=0; col<3; col++){
                 //RETRIEVE DUE DATE
                res = rs.rows.item(index).due;
                stringVarD[index]=res;
            // }

         }
     } else {
         res = "Unknown";
     }
  })
    return stringVarD;
}

function getPriority(date) {
   var db = getDatabase();
   var res="";
   var stringVarP=new Array();

   db.transaction(function(tx){
     var rs = tx.executeSql('SELECT * FROM todoB WHERE date=?;', [date]);
     if (rs.rows.length > 0) {
         for(var  index=0; index<rs.rows.length; index++){
            // for(var  col=0; col<3; col++){
                 //RETRIEVE PRIORITY
                res = rs.rows.item(index).priority;
                 stringVarP[index]=res;
                 //console.log("PRIORITY: " + res);

                // console.log("DERP:" + stringVar[index]);
            // }
         }
     } else {
         res = "Unknown";
     }
  })
    return stringVarP;
}
//DAN
